<template>
  <div class="login-container">
    <div class="login-form">
      <h2>登录</h2>
      <form id="loginForm" @submit.prevent="login">
        <div class="input-group">
          <input type="text" v-model="username" placeholder="用户名" required>
          <i class="fa-solid fa-user"></i>
        </div>
        <div class="input-group">
          <input type="password" v-model="password" placeholder="密码" required>
          <i class="fa-solid fa-lock"></i>
        </div>
        <button type="submit" class="btn-login">登录</button>
        <div class="forgot-password">
          <a href="#">忘记密码?</a>
        </div>
      </form>
      <div class="alert alert-danger mt-3" :style="{ display: errorMessage ? 'block' : 'none' }">
        用户名或密码错误
      </div>
    </div>
    <div class="custom-logo-section">
      <img src="@/assets/logo.png" alt="中盈优创交付考试系统" class="custom-logo">
      <p>中盈优创交付考试系统</p>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const username = ref('');
const password = ref('');
const errorMessage = ref('');

const login = () => {
  const data = {
    username: username.value,
    password: password.value
  };

  const formData = new URLSearchParams();
  for (const key in data) {
    formData.append(key, data[key]);
  }

  fetch('/user/login', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    body: formData.toString()
  })
    .then((response) => {
      if (response.ok) {
        return response.json();
      } else {
        throw new Error('网络请求失败');
      }
    })
    .then((result) => {
      if (result.code === 0) {
        // 登录成功，跳转到主页或其他页面
        window.location.href = '/';
      } else {
        // 登录失败，显示错误信息
        errorMessage.value = '用户名或密码错误';
      }
    })
    .catch((error) => {
      console.error('登录请求出错:', error);
      errorMessage.value = '用户名或密码错误';
    });
};
</script>

<style scoped>
body {
  margin: 0;
  padding: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
  background-size: 400% 400%;
  animation: gradient 15s ease infinite;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

@keyframes gradient {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

.login-container {
  background-color: white;
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
  padding: 40px;
  width: 800px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}

.login-form {
  flex: 1;
  margin-right: 20px;
}

.login-container h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

.input-group {
  position: relative;
  margin-bottom: 25px;
}

.input-group input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ccc;
  border-radius: 5px;
  font-size: 16px;
}

.input-group i {
  position: absolute;
  top: 50%;
  right: 15px;
  transform: translateY(-50%);
  color: #999;
}

.btn-login {
  width: 100%;
  padding: 12px;
  background-color: #007BFF;
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.btn-login:hover {
  background-color: #0056b3;
}

.forgot-password {
  text-align: right;
  margin-top: 10px;
}

.forgot-password a {
  color: #007BFF;
  text-decoration: none;
}

.forgot-password a:hover {
  text-decoration: underline;
}

.custom-logo-section {
  flex: 1;
  text-align: center;
  border-left: 1px solid #ccc;
  padding-left: 20px;
}

.custom-logo-section p {
  margin-bottom: 15px;
  color: #666;
  font-size: 20px;
  font-weight: bold;
}

.custom-logo {
  max-width: 200px;
  height: auto;
  margin-top: 20px;
}
</style>